<?
	include("includes/config.php");
	include("includes/login.php");
	include("includes/functions.php");
	$id_utilizator = $_SESSION['id_utilizator'];
	$msg = "";
	if(@$_POST['update']){
		$id_pct = $_POST['id_pct'];
		$nume_pct = $_POST['nume'];
		$adresa = $_POST['adresa'];
		mysql_query("update setari_punct_lucru set nume_punct_lucru = '$nume_pct', adresa = '$adresa' where id_punct_lucru = '$id_pct'  ");
	}
	if(@$_POST['sterge']){
		$id_pct = $_POST['id_pct'];
		mysql_query("delete from setari_punct_lucru where id_punct_lucru = '$id_pct'  ");
	}
	if(@$_POST['adauga']){
		$pct_lucru = $_POST['nume_pct_lucru'];
		$adresa_pct = $_POST['adresa_pct'];
		mysql_query("insert into setari_punct_lucru (id_punct_lucru,id_unitate,nume_punct_lucru,adresa) values(NULL,'$id_utilizator','$pct_lucru','$adresa_pct') ");
	}
	
	
	if(@$_GET['sterge_mecanic']){
		$id_mec = $_GET['sterge_mecanic'];
		mysql_query("delete from setari_mecanici where id_mecanic = '$id_mec' ");
	}
	if(@$_POST['adauga_mecanic']){
		$nume = $_POST['nume_mecanic'];
		$id_pct_lucru = $_POST['id_punct_lucru'];
		mysql_query("insert into setari_mecanici (id_mecanic,id_pct_lucru,nume_mecanic) values(NULL,'$id_pct_lucru','$nume') ") or die(mysql_errror());
		$msg = "Mecanic adaugat cu succes!";
	}
?>
<html>
	<head>
		<link rel="stylesheet" type="text/css" href="css/style.css" />
	</head>
	<body>
		<div id="header"><h1> <? echo detaliFirma('nume_firma'); ?></h1></div>
		<div id="warper">
			<?
				$resMeniu = mysql_query("select * from meniu");
			?>
			<div id="menu">
				<ul>
					<? while($rowMen = mysql_fetch_array($resMeniu)): ?>
					<li><a href="<?echo $rowMen['link'];?>"><? echo $rowMen['numeLink']; ?></a></li>
					<? endwhile; ?>
				</ul>
			</div>
			<div id="content">
				<div id="contentWrap">
					<div class="block">
						<a  class="buton" href="/settings.php">Setari Unitate</a>&nbsp; 
						<a class="buton" href="punctlucru.php">Setari puncte lucru</a>
						<a class="buton" href="/preturi.php">Setari preturi</a>
					</div>
					<div class="block">
						<? if($msg != ""): ?>
						<h1 style="color:#55ff00"><? echo $msg ?></h1>
						<? endif; ?>
						
						<h1>Punct De Lucru </h1>
						<?
							$res = mysql_query("select * from setari_punct_lucru where id_unitate = '$id_utilizator' ");
						?>
						
							<table  class="block">
							<tr id="capTabel">
								<td>Punct lucru</td>
								<td>Adresa</td>
								<td>Modifica</td>
							</tr>
							<? while($row = mysql_fetch_array($res)): ?>
							<tr>
								<form action=" " method="post">
								<input type="hidden" name="id_unitate" value="<? echo $row['id_unitate'];?>">
								<input type="hidden" name="id_pct" value="<? echo $row['id_punct_lucru'];?>">
								<td align="center"><input type="text" class="text" value="<? echo $row['nume_punct_lucru']; ?>" name="nume"></td>
								<td align="center"><input type="text" class="text" value="<? echo $row['adresa']; ?>" name="adresa"></td>
								<td>
									<input type="submit" class="buton" name="update" value="Modifica">
								</form>
								<form action="" method="post" style="float:right;padding:0;margin:0">
											<input type="hidden" name="id_pct" value="<? echo $row['id_punct_lucru'];?>">
											<input type="submit" class="buton" name="sterge" value="Sterge">	
								</form>
								</td>
							</tr>
							<? endwhile; ?>
						</table>
						
					<h1>Adauga Punct Lucru</h1>
					<form action="" method="post">
						<p>
							<label>Punct Lucru</label>
							<input type="text" class="text" name="nume_pct_lucru" value="">
						</p>
						<p>
							<label>Adresa Punct Lucru</label>
							<input type="text" class="text" name="adresa_pct" value="">
						</p>
						<input type="submit" class="buton" name="adauga" value="Adauga punct de lucru">	
					</form>	
					<div class="block"></div>
					
					
					<h1>Mecanici </h1>
					<form action="" method="post">
						<label>Punct Lucru</label>
						<select style="width:300px;height:26px;" name="id_punct_lucru">
							<? 
								$respct = mysql_query("select * from setari_punct_lucru where id_unitate='$id_utilizator'");
								while($rowpct = mysql_fetch_array($respct)):
							?>
							<option value="<? echo $rowpct['id_punct_lucru']; ?>"><? echo $rowpct['nume_punct_lucru']; ?></option>
							<? endwhile; ?>
						</select>
						<input class="buton" type="submit" value="schimba punct de lucru" name="change_punct_lucru">
					</form>
					
					
					<table>
						<a name="sterge_mecanic"></a>
						<tr id="capTabel">
							<td style="width:20px;">Nr.</td>
							<td style="width:500px;">Nume Mecanic.</td>
							<td style="width:100px;">Edit</td>
						</tr>
						
						<?
							if(@$_POST['change_punct_lucru']){
								$idPct = $_POST['id_punct_lucru'];
							}else{
								$respctm = mysql_query("select id_punct_lucru from setari_punct_lucru where id_unitate = '$id_utilizator'");
								$rowpctm = mysql_fetch_array($respctm);
								$idPct = $rowpctm['id_punct_lucru'];
					
							}
							$resmec = mysql_query("select * from setari_mecanici where id_pct_lucru = '$idPct' ");
							while($rowmec = mysql_fetch_array($resmec)):
						?>
						<tr style="height:25px;">
							<td style="width:20px;"><? echo $rowmec['id_mecanic']; ?></td>
							<td style="width:500px;"><? echo $rowmec['nume_mecanic']; ?></td>
							<td style="width:100px;text-align:center;"><a class="buton" href="/punctlucru.php?sterge_mecanic=<? echo $rowmec['id_mecanic']; ?>#sterge_mecanic">Sterge </a></td>
						</tr>
						<? endwhile; ?>
					</table>
					<br/>
					<form action="" method="post">
						<p>
							<label>Nume Mecanic</label>
							<input type="text" class="text" value="" name="nume_mecanic">
						</p>
						<p>
							<label>Punct de lucru</label>
							<select style="width:280px;height:26px;margin-top:5px;display:block" name="id_punct_lucru" >
							<? 
								$respct = mysql_query("select * from setari_punct_lucru where id_unitate='$id_utilizator'");
								while($rowpct = mysql_fetch_array($respct)):
							?>
							<option value="<? echo $rowpct['id_punct_lucru']; ?>"><? echo $rowpct['nume_punct_lucru']; ?></option>
							<? endwhile; ?>
						</select>
						</p>
						<input type="submit" name="adauga_mecanic" value="Adauga Mecanic" class="buton" >
					</form>
					
					
				</div>
			
			</div>
		</div>
	</body>
</html>